package com.yndsoft.jc.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseFragment;
import com.yndsoft.jc.model.Ad;
import com.yndsoft.jc.model.News;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.ui.categorypage.ProductDetailActivity;
import com.yndsoft.jc.ui.categorypage.ProductSearchActivity;
import com.yndsoft.jc.ui.memberpage.setting.AboutUsActivity;
import com.yndsoft.jc.ui.memberpage.setting.HelpCenterActivity;
import com.yndsoft.jc.ui.memberpage.setting.MonitorFlowDetailActivity;
import com.yndsoft.jc.ui.memberpage.setting.MonitoringGuideActivity;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;
import com.yndsoft.jc.widget.AutoSwipeRefreshLayout;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

//import com.yndsoft.jc.ui.homepage.HomeAutoVPAdapter;

/**
 * Created by Administrator on 2017-01-09.
 */
public class IndexFragment extends BaseFragment implements View.OnClickListener {
    @Bind(R.id.homepage_swipe)
    AutoSwipeRefreshLayout homepage_swipe;
    @Bind(R.id.homepage_scrollView)
    ScrollView homepage_scrollView;

    @Bind(R.id.homepage_search)
    TextView homepage_search;

    @Bind(R.id.homeview_pager)
    RollPagerView homeview_pager;

    @Bind(R.id.image_1075)
    ImageView image_1075;
    @Bind(R.id.image_1076)
    ImageView image_1076;
    @Bind(R.id.image_1077)
    ImageView image_1077;
    @Bind(R.id.image_1078)
    ImageView image_1078;

    @Bind(R.id.homepage_proprietary_technology)
    LinearLayout homepage_proprietary_technology;//专利技术
    @Bind(R.id.homepage_check_up)
    LinearLayout homepage_check_up;//服务流程
    @Bind(R.id.homepage_organization)
    LinearLayout homepage_organization;//在线下单
    @Bind(R.id.saled_service)
    LinearLayout saled_service;//客户服务
    @Bind(R.id.homepage_standard)
    LinearLayout homepage_standard;//常见问题

    //下拉刷新为0， 上划加载更多为1
    private int step = 0;
    private Activity context;
    private View root;
    private List<String> imageUrl = new ArrayList<>();
    private ImageLoopAdapter adapter;


    @Override
    public int getContentResouceId() {
        return R.layout.fragment_index;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();

        getDatas();
        setViews();
        setListener();
        homepage_swipe.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
    }

    private List<Ad> ad_list;

    private void getDatas() {
        adapter = new ImageLoopAdapter(homeview_pager);
        mHttpUtil.getData(new ProgressSubscriber<ReturnMsg>(context) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:

                        break;
                    case 1:
                        Type type = new TypeToken<List<Ad>>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        ad_list = (ArrayList<Ad>) fromJson;
                        for (int i = 0; i < ad_list.size(); i++) {
                            imageUrl.add(ad_list.get(i).getImg());
                        }
                        adapter.notifyDataSetChanged();
                        if (homepage_swipe != null) {
                            homepage_swipe.setRefreshing(false);
                        }
                        break;
                }
            }
        }, "ad", 1074);
        getAdImage(1075, image_1075);
        getAdImage(1076, image_1076);
        getAdImage(1077, image_1077);
        getAdImage(1078, image_1078);
    }

    private String img;

    private void getAdImage(int pid, final ImageView imageView) {
        mHttpUtil.getData(new ProgressSubscriber<ReturnMsg>(context) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:

                        break;
                    case 1:
                        Type type = new TypeToken<List<Ad>>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        final List<Ad> list = (ArrayList<Ad>) fromJson;
                        img = list.get(0).getImg();
                        Glide.with(context).load(img).into(imageView);
                        imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = null;
                                if (list.get(0).getOperatetype() == 1) {//产品
                                    intent = new Intent(context, ProductDetailActivity.class);
                                    intent.putExtra(ConstantValues.ID_AD, list.get(0).getOperatetypeid());
                                    startActivity(intent);
                                } else if (list.get(0).getOperatetype() == 2) {//新闻
                                    getnews(list.get(0).getOperatetypeid());
                                } else if (list.get(0).getOperatetype() == 3) {//帮助
                                    intent = new Intent(context, HelpCenterActivity.class);
                                    startActivity(intent);
                                } else {//配置
                                    intent = new Intent(context, AboutUsActivity.class);
                                    intent.putExtra(ConstantValues.ABOUT_AD, list.get(0).getOperatetypeid());
                                    startActivity(intent);
                                }
                            }
                        });
                        if (homepage_swipe != null) {
                            homepage_swipe.setRefreshing(false);
                        }
                        break;
                }
            }
        }, "ad", pid);
    }

    @Override
    public String getFragmentName() {
        return null;
    }

    private void setViews() {
        homepage_swipe.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
        homeview_pager.setAdapter(adapter);
    }

    private void setListener() {
        homepage_swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getDatas();
            }
        });
        homeview_pager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = null;
                if (ad_list.get(position).getOperatetype() == 1) {//产品
                    intent = new Intent(context, ProductDetailActivity.class);
                    intent.putExtra(ConstantValues.ID_AD, ad_list.get(position).getOperatetypeid());
                    startActivity(intent);
                } else if (ad_list.get(position).getOperatetype() == 2) {//新闻
                    getnews(ad_list.get(position).getOperatetypeid());
                } else if (ad_list.get(position).getOperatetype() == 3) {//帮助
                    intent = new Intent(context, HelpCenterActivity.class);
                    startActivity(intent);
                } else {//配置
                    intent = new Intent(context, AboutUsActivity.class);
                    intent.putExtra(ConstantValues.ABOUT_AD, ad_list.get(position).getOperatetypeid());
                    startActivity(intent);
                }
            }
        });
        homepage_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, ProductSearchActivity.class));
            }
        });
        homepage_proprietary_technology.setOnClickListener(this);
        homepage_check_up.setOnClickListener(this);
        homepage_organization.setOnClickListener(this);
        saled_service.setOnClickListener(this);
        homepage_standard.setOnClickListener(this);
    }

    private void getnews(int id) {
        mHttpUtil.getNews(new ProgressSubscriber<ReturnMsg>(context) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        break;
                    case 1:
                        Type type = new TypeToken<News>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        News news = (News) fromJson;
                        Intent intent = new Intent(context, MonitorFlowDetailActivity.class);
                        intent.putExtra(MonitoringGuideActivity.GUID_FLOW_TITLE, news.getName());
                        intent.putExtra(MonitoringGuideActivity.GUID_FLOW_CONTENT, news.getNote());
                        startActivity(intent);
                        break;
                }
            }
        }, "news", id);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.homepage_proprietary_technology:
                intent = new Intent(context, AboutUsActivity.class);
                intent.putExtra(ConstantValues.ABOUT_AD, 10031);
                startActivity(intent);
                break;
            case R.id.homepage_check_up:
                intent = new Intent(context, AboutUsActivity.class);
                intent.putExtra(ConstantValues.ABOUT_AD, 10032);
                startActivity(intent);
                break;
            case R.id.homepage_organization:
                intent = new Intent(context, AboutUsActivity.class);
                intent.putExtra(ConstantValues.ABOUT_AD, 10033);
                startActivity(intent);
                break;
            case R.id.saled_service:
                intent = new Intent(context, AboutUsActivity.class);
                intent.putExtra(ConstantValues.ABOUT_AD, 10034);
                startActivity(intent);
                break;
            case R.id.homepage_standard:
                intent = new Intent(context, AboutUsActivity.class);
                intent.putExtra(ConstantValues.ABOUT_AD, 10035);
                startActivity(intent);
                break;
        }
    }


    private class ImageLoopAdapter extends LoopPagerAdapter {
//        int[] imgs = new int[]{
//                R.drawable.qqq,
//                R.drawable.www,
//                R.drawable.eee,
//                R.drawable.rrr,
//        };

        public ImageLoopAdapter(RollPagerView viewPager) {
            super(viewPager);
        }

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            view.setImageResource(imgs[position]);
            Glide.with(getActivity()).load(imageUrl.get(position)).into(view);
            return view;
        }

        @Override
        public int getRealCount() {
            return imageUrl.size();
        }
    }
}